//
//  NSAttributedString+Extensions.swift
//  扩展文件
//
//  Created by 伏文东 on 2018/2/5.
//  Copyright © 2018年 伏文东. All rights reserved.
//

import UIKit

extension NSAttributedString {
    /// 使用图像和文本生成上下排列的属性文本
    ///
    /// - Parameters:
    ///   - image:       图像
    ///   - imageWH:     图像宽高
    ///   - title:       标题文字
    ///   - fontSize:    标题文字大小
    ///   - titleColor:  标题颜色
    ///   - spacing:     图像和标题间距
    /// - Returns:       属性文本
    static func imageTextWithImage(image: UIImage, imageWH: CGFloat, title: String, fontSize: CGFloat, titleColor: UIColor, spacing: CGFloat) -> NSAttributedString {
        /// 文本字典
        let titleDict = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: fontSize),
                         NSAttributedStringKey.backgroundColor: titleColor]
        let spacingDict = [NSAttributedStringKey.font: UIFont.systemFont(ofSize: spacing)]
        
        /// 图像文本
        let attachment = NSTextAttachment()
        attachment.image = image
        attachment.bounds = CGRect(x: 0, y: 0, width: imageWH, height: imageWH)
        let imageText = NSAttributedString.init(attachment: attachment)
        
        let lineText = NSAttributedString.init(string: "\n \n", attributes: spacingDict)
        
        let text = NSAttributedString.init(string: title, attributes: titleDict)
        
        let attM = NSMutableAttributedString(attributedString: imageText)
        
        attM.append(lineText)
        attM.append(text)
        
        return attM.copy() as! NSAttributedString
    
    }
}































